<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller;

use app\api\model\ding\Seat as SeatModel;
use app\api\model\ding\ShopCollect as ShopCollectModel;
use app\api\model\store\Shop as ShopModel;
use app\api\model\store\shop\Diy as ShopDiyModel;
use app\api\service\User as UserService;
use think\facade\Db;
use app\common\enum\ding\Ribao as SceneEnum;
use app\common\service\qrcode\PosterNeibu as OrderPosterOut;
/**
 * 门店列表
 * Class Shop
 * @package app\api\controller
 */
class Shop extends Controller
{

    public function advSetting(int $shopId = 0)
    {
        $data = $this->postForm();
        if ($data['show_adv'] == true) {
            Db::name('ding_shop_adv_images')->where('shop_id', $shopId)->delete();

            Db::name('store_shop')->where('shop_id', $shopId)
                ->update(['float_id' => $data['float_id'], 'show_adv' => $data['show_adv'] == true ? 1 : 0]);
            $adv['store_id']    = 10001;
            $adv['create_time'] = time();
            $adv['shop_id']     = $shopId;
            $adv['file_id']     = $data['adv_id'];
            Db::name('ding_shop_adv_images')->insert($adv);
        } else {

            Db::name('store_shop')->where('shop_id', $shopId)
                ->update(['show_adv' => 0]);

        }

        return $this->renderSuccess([], '添加成功');
    }

    //转发记录-新增

    public function addShareLog($shopId)
    {
        Db::name('ding_shop_visit')->insert(['shop_id' => $shopId, 'create_time' => time()]);
        return $this->renderSuccess([], '添加成功');
    }

    //邀请函记录-新增

    public function addShare($shopId, $clerkId = 0)
    {
        Db::name('store_shop')->where('shop_id', $shopId)->inc('share_views', 1)->inc('save_money', 3)->update();

        //节省金额，记录
        $data['shop_id']     = $shopId;
        $data['clerk_id']    = $clerkId;
        $data['value']       = 3;
        $data['store_id']    = 10001;
        $data['create_time'] = time();
        $data['describe']    = '您的顾客转发了邀请函给他的好友，为您门店节省了3元的宣传费用。';
        Db::name('store_shop_make_log')->insert($data);

        return $this->renderSuccess([], '添加成功');
    }
    //商家访问记录-新增

    public function addView($shopId, $clerkId = 0)
    {
        Db::name('store_shop')->where('shop_id', $shopId)->inc('views', 1)->update();
        $data['shop_id']  = $shopId;
        $data['clerk_id'] = $clerkId;

        $data['store_id'] = 10001;

        Db::name('store_shop_view')->insert($data);

        return $this->renderSuccess([], '添加成功');
    }
    /*
		 *收藏记录
	*/
    public function collectList()
    {
        $model = new ShopCollectModel;
        $list  = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }
    /**
     * 门店列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list()
    {
        $model = new ShopModel;
        $list  = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }
    /*
	 *推广数据
	 */

    public function getTuiGuangData(int $shopId = 0)
    {

        //$view_count = Db::name('ding_visit_log')->where('shop_id', $shopId)->count();
        $shop = Db::name('store_shop')->where('shop_id', $shopId)->field('views,liebian_count')->find();
        //$share_count = Db::name('ding_shop_visit')->where('shop_id', $shopId)->count();
        //当前月
        $time=strtotime(Date('Y-m-01'));
        $view_count  = Db::name('ding_ribao')
        ->where('create_time','>',$time)
        ->where('shop_id', $shopId)
        ->where('clerk_id',0)
        ->where('scene', SceneEnum::YQHBGL)->sum('value');
        $share_count = Db::name('ding_ribao') ->where('create_time','>',$time)->where('shop_id', $shopId)->where('scene', SceneEnum::LBYHS)->sum('value');

        return $this->renderSuccess(compact('view_count', 'share_count', 'shopId'));
    }
    /*
		*推荐专区
	*/

    public function getShopListTuijian($city = '')
    {
        $model = new ShopModel;
        $list  = $model->getListZhuanqu($this->request->param());
        return $this->renderSuccess(compact('list'));
    }
    public function getCollect(int $shopId = 0)
    {
        $userId = UserService::getCurrentLoginUserId(true);
        $model  = ShopCollectModel::detail($shopId, $userId);
        $status = $model ? 1 : 0;

        return $this->renderSuccess(compact('status'));
    }
    public function ai(int $shopId = 0)
    {
        $data=$this->postForm();
        
        $detail=Db::name('store_shop')->where('shop_id',$shopId)->field('shop_name')->find();
        $ai=Db::name('store')->where('store_id',10001)->value('ai');

        $ai=str_replace('包厢名称', $data['name'], $ai);
        $ai=str_replace('当前店员的手机号', $data['mobile'], $ai);
        $ai=str_replace('餐厅名称', $detail['shop_name'], $ai);
        $ai=str_replace('赵先生', $data['real_name'], $ai);



        return $this->renderSuccess(compact('detail','ai'));
    }
    /**
     * 门店详情
     * @param int $shopId
     * @return array
     */
    public function detail(int $shopId = 0)
    {
        $clerkId = 0;
        if ($shopId == 0) {
            $user    = UserService::getCurrentLoginUser(true);
            $clerk   = Db::name('store_shop_clerk')->where('user_id', $user['user_id'])->field('shop_id,clerk_id')->find();
            $shopId  = $clerk['shop_id'];
            $clerkId = $clerk['clerk_id'];
        }
        $detail = ShopModel::detail($shopId, ['logoImage', 'floatImage', 'advImages.file', 'vrImage', 'images.file', 'types.type', 'foods.food', 'dish.images', 'vrVideo','parkImage']);

        return $this->renderSuccess(compact('detail', 'clerkId'));
    }
    public function diyDetail(int $shopId = 0)
    {

        $detail = ShopModel::detail($shopId);
        $title  = $detail['diy_title'];
        $model  = new ShopDiyModel;
        $list   = $model->getList($shopId);

        return $this->renderSuccess(compact('title', 'list'));
    }
    /*
		*包含座位
	*/
    public function detailAll(int $shopId = 0)
    {
        if ($shopId == 0) {
            $user   = UserService::getCurrentLoginUser(true);
            $shopId = Db::name('store_shop_clerk')->where('user_id', $user['user_id'])->value('shop_id');
        }

        $detail = ShopModel::detail($shopId, ['logoImage', 'vrImage', 'images.file', 'types.type', 'foods.food', 'dish.images']);

        //是否打烊
        $start_time = strtotime(Date('Y-m-d ') . $detail['start_time']);
        $end_time   = strtotime(Date('Y-m-d ') . $detail['end_time']);
        if (time() >= $start_time && $end_time >= time()) {
            $detail['is_close'] = 0;
        } else {
            $detail['is_close'] = 1;
        }
        $model = new SeatModel;
        //包厢
        $box = $model->getList(1, $shopId);

        //卡座
        $booth = $model->getList(2, $shopId);

        if ($box) {
            $tabList[] = ['name' => '包厢', 'value' => 1];
        }
        if ($booth) {
            $tabList[] = ['name' => '卡座', 'value' => 2];
        }
        $tabList[] = ['name' => '菜品', 'value' => 3];
        $tabList[] = ['name' => '店内环境', 'value' => 4];

        return $this->renderSuccess(compact('detail', 'box', 'booth', 'tabList'));
    }
    public function bind(int $shopId)
    {
        $detail = ShopModel::detail($shopId);
        $detail->bind($this->postForm());
        return $this->renderSuccess('绑定成功');
    }

    public function edit(int $shopId)
    {
        $model = ShopModel::detail($shopId);
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess([], '更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
    public function diySave(int $shopId)
    {
        $model = ShopModel::detail($shopId);
        if ($model->diySave($this->postForm())) {
            return $this->renderSuccess([], '更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    public function setting(int $shopId)
    {
        $model = ShopModel::detail($shopId);
        if ($model->setting($this->postForm())) {
            return $this->renderSuccess([], '设置成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
    /*
		 *收藏处理
		 */
    public function collectSave(int $shopId)
    {
        $userId = UserService::getCurrentLoginUserId(true);
        $model  = ShopCollectModel::detail($shopId, $userId);
        if ($model) {
            $model->del();
            return $this->renderSuccess(['status' => 0], '已取消收藏');
        } else {
            $model = new ShopCollectModel;
            $model->add($shopId, $userId);
            return $this->renderSuccess(['status' => 1], '收藏成功');
        }

    }
    /*
		 *收藏批量删除
		 */
    public function collectDel()
    {

        $model = new ShopCollectModel;
        $model->delAll($this->postForm());
        return $this->renderSuccess([], '批量删除成功');

    }

}
